import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:38917
 * Date:2025/4/20
 * Time:09:51
 */
public class ThreadLocal_Demo {
    /*private String content;

    public String getContent() {
        return content;

    }

    public void setContent(String content) {
        this.content = content;
    }

    public static void main(String[] args) {
        ThreadLocal_Demo threadLocalDemo = new ThreadLocal_Demo();
        for (int i = 0; i < 10; i++) {
            Thread thread = new Thread(() -> {
                threadLocalDemo.setContent(Thread.currentThread().getName() + "的数据");
                System.out.println(Thread.currentThread().getName() + "------>" + threadLocalDemo.getContent());
            });
            thread.start();
        }
    }*/



    /*private static final ThreadLocal<String> threadLocal = new ThreadLocal<String>(){
        @Override
        protected String initialValue() {
            return "初始值";
        }
    };
    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            Thread thread = new Thread(() -> {
                //threadLocal.set(Thread.currentThread().getName());
                System.out.println(Thread.currentThread().getName() + "------>" + threadLocal.get());
            });
            thread.start();
        }
    }*/


    
    public static void main(String[] args) {
        ThreadLocal<String> threadLocal = new ThreadLocal<>();
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(4,4,0, TimeUnit.MINUTES,
                new ArrayBlockingQueue<>(4),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.CallerRunsPolicy());
        for (int i = 0; i < 4; i++) {
            threadPoolExecutor.execute(() -> {
                threadLocal.set(Thread.currentThread().getName());
                System.out.println(Thread.currentThread().getName() + "------>" + threadLocal.get());
            });
        }
    }
}
